﻿using MrdocUtils.Model;
using Newtonsoft.Json;
using System.Net;
using System.Reflection;
using System.Text;

namespace MrdocUtils
{
    internal class Program
    {
        public static int id = 1;

        private static void Main(string[] args)
        {


        }



        public static void GetYaml(ref StringBuilder result, string root)
        {
            var rootDirs = Directory.GetDirectories(root);
            if (rootDirs.Any())
            {
                foreach (var dir in rootDirs)
                {
                    File.Create(dir + ".md");
                    GetYaml(ref result, dir);
                }
            }

            var files = Directory.GetFiles(root);
            if (!files.Any()) return;
            result.Append("- children:");
            result.Append('\n');

            foreach (var file in files)
            {
                if (Path.GetExtension(file) != ".md") continue;
                result.Append("  - file: ");
                result.Append(Path.GetFileName(file));
                result.Append('\n');
                result.Append("    name: ");
                result.Append(Path.GetFileNameWithoutExtension(file));
                result.Append('\n');
                result.Append($"    id: {id}\n");
            }
            result.Append($"  file: {Path.GetFileName(root)}.md\n");
            result.Append($"  name: {Path.GetFileNameWithoutExtension(root)}\n");
            result.Append($"  id: {id}\n");
            id++;

        }

        public static void GetYamlJson(ref Book result, string root)
        {
            var rootDirs = Directory.GetDirectories(root);
            if (rootDirs.Any())
            {
                foreach (var dir in rootDirs)
                {
                    File.Create(dir + ".md");
                    GetYamlJson(ref result, dir);
                }
            }

            var files = Directory.GetFiles(root);
            if (!files.Any()) return;

            var chapter = new Chapter
            {
                Title = Path.GetFileName(root),
                ChapterPath = root,
                Id = id
            };
            foreach (var file in files)
            {
                if (Path.GetExtension(file) != ".md") continue;
                var chapter2 = new Chapter
                {
                    Title = Path.GetFileName(root),
                    ChapterPath = root,
                    Id = id
                };
                chapter.Chirden.Add(chapter);
            }
            result.Chapters.Add(chapter);
            id++;
        }
    }
}

